package com.hospital.ssm.controller;

import com.hospital.ssm.model.HospitalType;
import com.hospital.ssm.model.ResultInfo;
import com.hospital.ssm.service.HospitalTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class HospitalTypeController {

    @Autowired
    private HospitalTypeService hospitalTypeService;

    @GetMapping("/hospitalTypes")
    public String hospitalTypeList(Integer id, Model model) {
        try {
            List<HospitalType> hospitalTypes = hospitalTypeService.hospitalTypeList(id);
            model.addAttribute("hospitalTypes", hospitalTypes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "hospitaltypemanagement";
    }

    @ResponseBody
    @PostMapping("/newhospitalTypes")
    public ResultInfo hospitalTypeAdd(@RequestParam("htName") String htName, @RequestParam("htStatus") Integer htStatus, HttpServletRequest request) {
        ResultInfo info = new ResultInfo();
        HospitalType hospitalType = new HospitalType();
        HttpSession session = request.getSession();
        hospitalType.setCreateMan((String) session.getAttribute("username"));
        hospitalType.setHtName(htName);
        hospitalType.setHtStatus(htStatus);
        try {
            hospitalTypeService.hospitalTypeAdd(hospitalType);
            info.setOk();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    @ResponseBody
    @PutMapping("/rehospitalTypes")
    public ResultInfo hospitalTypeUpdate(@RequestParam("htId") Integer htId, @RequestParam("htName") String htName, @RequestParam("htStatus") Integer htStatus, HttpServletRequest request) {
        ResultInfo info = new ResultInfo();
        HospitalType hospitalType = new HospitalType();
        HttpSession session = request.getSession();
        hospitalType.setUpdateMan((String) session.getAttribute("username"));
        hospitalType.setHtName(htName);
        hospitalType.setHtStatus(htStatus);
        hospitalType.setHtId(htId);

        try {
            hospitalTypeService.hospitalTypeUpdate(hospitalType);
            info.setOk();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    @ResponseBody
    @PostMapping("/delhospitalTypes")
    public ResultInfo hospitalTypeDelete(@RequestBody @RequestParam("idList") List<String> idList) {
        ResultInfo info = new ResultInfo();
        if (idList.size() == 1) {
            String item = idList.get(0);
            item = item.substring(2, item.length() - 2);
            int id = Integer.parseInt(item);
            try {
                hospitalTypeService.hospitalTypeDelete(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return info;
        }

        for (int i = 0; i < idList.size(); i++) {
            String item = idList.get(i);
            if (i == 0) {
                item = item.substring(1);

            } else if (i == idList.size() - 1) {
                item = item.substring(0, item.length() - 1);
            }
            item = item.substring(1, item.length() - 1);
            int id = Integer.parseInt(item);
            try {
                hospitalTypeService.hospitalTypeDelete(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return info;
    }

    @ResponseBody
    @PatchMapping("/usehospitalTypes")
    public ResultInfo hospitalTypeisuse(@RequestBody @RequestParam("idList") List<String> idList, @RequestParam("htStatus") Integer htStatus) {
        ResultInfo info = new ResultInfo();
        HospitalType hospitalType = new HospitalType();
        hospitalType.setHtStatus(htStatus);

        if (idList.size() == 1) {
            String item = idList.get(0);
            item = item.substring(2, item.length() - 2);
            int id = Integer.parseInt(item);
            hospitalType.setHtId(id);
            try {
                hospitalTypeService.hospitalTypeUpdatehtStatus(hospitalType);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return info;
        }

        for (int i = 0; i < idList.size(); i++) {
            String item = idList.get(i);
            if (i == 0) {
                item = item.substring(1);

            } else if (i == idList.size() - 1) {
                item = item.substring(0, item.length() - 1);
            }
            item = item.substring(1, item.length() - 1);
            int id = Integer.parseInt(item);
            hospitalType.setHtId(id);
            try {
                hospitalTypeService.hospitalTypeUpdatehtStatus(hospitalType);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return info;
    }
}